
#include <string.h>
#include <jni.h>
#include "encrypt/ed_signature.h"

jstring GPN(JNIEnv *env, jobject thiz, jobject context)
{
    //Context的类
    jclass context_clazz = (*env)->GetObjectClass(env, context);
    // 得到 getPackageName 方法的 ID
    jmethodID methodID_pack = (*env)->GetMethodID(env, context_clazz,
                                                  "getPackageName", "()Ljava/lang/String;");
    // 获得当前应用的包名
    jstring application_package = (*env)->CallObjectMethod(env, context, methodID_pack);
    return application_package;
}

jstring GSS(JNIEnv *env, jobject clazz, jobject context)
{
    //Context的类
    jclass context_clazz = (*env)->GetObjectClass(env, context);
    // 得到 getPackageManager 方法的 ID
    jmethodID methodID_getPackageManager = (*env)->GetMethodID(env,
                                                               context_clazz, "getPackageManager",
                                                               "()Landroid/content/pm/PackageManager;");
    // 获得PackageManager对象
    jobject packageManager = (*env)->CallObjectMethod(env, context, methodID_getPackageManager);
    // 获得 PackageManager 类
    jclass pm_clazz = (*env)->GetObjectClass(env, packageManager);
    // 得到 getPackageInfo 方法的 ID
    jmethodID methodID_pm = (*env)->GetMethodID(env, pm_clazz, "getPackageInfo",
                                                "(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;");
    // 得到 getPackageName 方法的 ID
    jmethodID methodID_pack = (*env)->GetMethodID(env, context_clazz,
                                                  "getPackageName", "()Ljava/lang/String;");

    // 获得当前应用的包名
    jstring application_package = (*env)->CallObjectMethod(env, context, methodID_pack);

    // 获得PackageInfo
    jobject packageInfo = (*env)->CallObjectMethod(env, packageManager, methodID_pm, application_package, 64);

    jclass packageinfo_clazz = (*env)->GetObjectClass(env, packageInfo);
    jfieldID fieldID_signatures = (*env)->GetFieldID(env, packageinfo_clazz,
                                                     "signatures", "[Landroid/content/pm/Signature;");

    jobjectArray signature_arr = (jobjectArray) (*env)->GetObjectField(env, packageInfo, fieldID_signatures);
    //Signature数组中取出第一个元素
    jobject signature = (*env)->GetObjectArrayElement(env, signature_arr, 0);
    //读signature的hashcode
    jclass signature_clazz = (*env)->GetObjectClass(env, signature);

    jmethodID tocharStringId = (*env)->GetMethodID(env, signature_clazz, "toCharsString", "()Ljava/lang/String;");

    jstring signatureStr = (*env)->CallObjectMethod(env, signature, tocharStringId);
    return signatureStr;
}
